/**
 * FileName: WorkerController
 * Author:   Leopold
 * Date:     2018/9/29 16:50
 * Description:
 * History:
 */
package cn.mcsys.viewer.core.web;


import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirWorker;
import cn.mcsys.myadmin.modules.worker.vo.WorkerIndexVO;
import cn.mcsys.viewer.core.utils.HttpRequestLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("worker")
public class WorkerController {

    @Autowired
    private BackService backService;

    @PostMapping("login")
    @ResponseBody
    public JsonResult login(String username, String password) {
        AirWorker airWorker = backService.workerLogin(username, password);
        if (airWorker == null) {
            return JsonResult.fail();
        }
        HttpRequestLocal.setWorkUserSession(airWorker);
//        查询所有的该站点车辆状态,和发车和候车的信息,乘车点的信息
        if("vip".equals(airWorker.getNote())){
            return JsonResult.success("vip");
        }
        return JsonResult.success();
    }

    /**
     * 工作人员端首页，接收工作人员的经纬度
     * @return
     */
    @GetMapping("index")
    @ResponseBody
    public JsonResult index(){
        AirWorker airWorker = HttpRequestLocal.getWorkUserSession();
        WorkerIndexVO workerIndexVO = backService.workerIndex(airWorker);
        System.out.println(workerIndexVO.toString());
        return JsonResult.success(workerIndexVO);
    }

    /**
     * 工作人员端首页，接收工作人员的经纬度
     * @return
     */
    @GetMapping("vipIndex")
    @ResponseBody
    public JsonResult vipIndex(Integer pointId){
        AirWorker airWorker = HttpRequestLocal.getWorkUserSession();
        if("vip".equals(airWorker.getNote())){
            WorkerIndexVO workerIndexVO = backService.workerVipIndex(pointId);
            return JsonResult.success(workerIndexVO);
        }
        return JsonResult.success();
    }

    @GetMapping("getRidePoints")
    @ResponseBody
    public JsonResult getAllPoint() {
        return JsonResult.success(backService.selectAllPoint2());
    }

    /**
     * 获取所有该站点的所有车辆信息
     * @return
     */
    @RequestMapping("getPointAllCar")
    @ResponseBody
    public JsonResult getPointAllCar() {
        AirWorker workUser = HttpRequestLocal.getWorkUserSession();
        List<AirCar> airCars = backService.selectByPointId(workUser.getAirRidePoint().getId());
        return JsonResult.success(airCars);
    }

    /**
     * 获取所有该站点的所有车辆信息
     * @return
     */
    @RequestMapping("getCarListByPointId")
    @ResponseBody
    public JsonResult getCarsList(Integer pointId) {
        List<AirCar> airCars = backService.selectByPointId(pointId);
        return JsonResult.success(airCars);
    }

    /**
     * 手动发车
     * @param airCarId
     * @param sum
     * @return
     */
    @RequestMapping("departure")
    @ResponseBody
    public JsonResult starting(Long airCarId, Integer sum) {
        AirWorker workerUser = HttpRequestLocal.getWorkUserSession();
//        设置车辆状态
        backService.updateCarStatus(airCarId, AirCar.RIDING);
//        车辆状态表
        backService.insertCarStartLog(workerUser, airCarId, AirCar.RIDING, sum);
//        新增实时消息记录
        backService.insertRealTimeMessage(workerUser, airCarId, AirCar.RIDING, sum);
        return JsonResult.success();
    }


    /**
     * 记录候车人数
     * @param sum
     * @return
     */
    @PostMapping("waiting")
    @ResponseBody
    public JsonResult saveWaitingSum(Integer sum){
        AirWorker workerUser = HttpRequestLocal.getWorkUserSession();
//        更新乘车点的候车人数
        backService.updateWaitingSum(workerUser.getAirRidePoint().getId(),sum);
//        新增实时消息记录
        backService.insertWaitingRealTimeMessage(workerUser, sum);
        return JsonResult.success();
    }

    /**
     * 发送消息记录
     * @param type
     * @param message
     * @return
     */
    @PostMapping("message")
    @ResponseBody
    public JsonResult sendMessage(Integer type ,String message){
        AirWorker airWorker = HttpRequestLocal.getWorkUserSession();
        //        新增实时消息记录
        backService.insertTypeRealTimeMessage(airWorker,type, message);
        return JsonResult.success();
    }

    /**
     * 获取系统消息(50条)
     *
     * @return
     */
    @GetMapping("message")
    @ResponseBody
    public JsonResult getMessage() {
        AirWorker userWorker = HttpRequestLocal.getWorkUserSession();
        return JsonResult.success(backService.getMessageByWorker(userWorker));
    }

    /**
     * 获取系统消息(50条)
     *
     * @return
     */
    @GetMapping("messageForVip")
    @ResponseBody
    public JsonResult getMessageByPointId(Integer pointId) {
        AirWorker userWorker = HttpRequestLocal.getWorkUserSession();
        if("vip".equals(userWorker.getNote())) {
            return JsonResult.success(backService.getMessageByPointId(pointId));
        }
        return JsonResult.success();
    }

    @GetMapping("searchCar")
    @ResponseBody
    public JsonResult searchCar(String carNumber){
        AirWorker userWorker = HttpRequestLocal.getWorkUserSession();
        List<AirCar> airCars = backService.selectLikeByCarNumber(userWorker.getAirRidePoint().getId(),carNumber);
        if(airCars == null || airCars.size() == 0){
            return JsonResult.failMessage("没有该车辆");
        }
        return JsonResult.success(airCars);
    }

}